package org.carpediem.conman.entity;

import org.carpediem.conman.Util;

import javax.persistence.*;
import java.net.URI;
import java.util.Date;
import java.util.Random;

/**
 * @author Mark Petrovic
 */
@Entity
public class Note extends DomainObject {
    public static final String TEXT = "text";

    private long id;
    private String note;
    private String s3Key = Util.hexDigest(s3Key());
    private URI uri;
    private long timestamp = new Date().getTime();
    private Contact contact;
    private Firm firm;
    private boolean persistedToS3 = false;

    @Id
    @GeneratedValue
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(nullable = false)
    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    @ManyToOne
    @JoinColumn(name = "contact_id")
    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public String getS3Key() {
        return s3Key;
    }

    public void setS3Key(String s3Key) {
        this.s3Key = s3Key;
    }

    public boolean isPersistedToS3() {
        return persistedToS3;
    }

    public void setPersistedToS3(boolean persistedToS3) {
        this.persistedToS3 = persistedToS3;
    }

    @Column(nullable = false)
    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    private String s3Key() {
        StringBuilder sb = new StringBuilder().append(new Random().nextInt()).append(new Date(timestamp)).append(hashCode());
        return Util.hexDigest(sb);
    }

    public URI getUri() {
        return uri;
    }

    public void setUri(URI uri) {
        this.uri = uri;
    }

    @ManyToOne
    @JoinColumn(name = "firm_id")
    public Firm getFirm() {
        return firm;
    }

    public void setFirm(Firm firm) {
        this.firm = firm;
    }
}
